package com.mind.customer.handler.zhenghua;

import com.alibaba.fastjson2.JSONArray;
import com.mind.api.vo.OrderInfoResponse;
import com.mind.customer.api.ocr.OCRUtils;
import com.mind.customer.config.FileConfig;
import com.mind.customer.domain.vo.HandlerData;
import com.mind.customer.domain.vo.TaskResult;
import com.mind.customer.exception.AjObjectNotFoundException;
import com.mind.customer.handler.common.CommonHandler;
import com.mind.system.domain.AjTicketTask;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发票处理器
 */
@Component
@Log4j2
public class ZhengHuaInvoiceHandler {

    @Resource
    private OCRUtils ocrUtils;

    @Resource
    private FileConfig fileConfig;

    @Resource
    private CommonHandler commonHandler;

    private String invoiceFieldKey = "发票";

    /**
     * 处理发票数据
     *
     * @param fileList
     * @return
     */
    public TaskResult handler(AjTicketTask task, List<OrderInfoResponse.FileList> fileList, Map<String, Map<String, Object>> params) throws AjObjectNotFoundException, IOException {
        JSONArray invoiceList = new JSONArray();
        for (OrderInfoResponse.FileList list : fileList) {
//            Field[] fields = list.getClass().getDeclaredFields();
//            ;
//            String pdfUrl = "";
//            if (info.containsKey("invoiceNumber") &&
//                    params.containsKey(info.get("invoiceNumber").toString())) {
//                Map<String, Object> contentPass = params.get(info.get("invoiceNumber").toString());
//                pdfUrl = contentPass.get("fileUrl").toString();
//                info.putAll((Map<String, Object>) contentPass.get("content"));
//            }
//            for (Field field : fields) {
//                field.setAccessible(true);
//                if (!field.getName().equals("invoiceInfo")) {
//                    try {
//                        info.put(field.getName(), field.get(list));
//                    } catch (IllegalAccessException e) {
//                        log.error("{}, 发票字段反射获取值，出现异常:", field.getName(), e);
//                    }
//                }
//            }
//            FileVo fileVo = null;
//            try {
////                if (StringUtils.isNotEmpty(info.get("imageUrl").toString())) {
////                    fileVo = FileUtils.download(info.get("imageUrl").toString(), fileConfig.getBasePath(), fileConfig.getBucketName());
////                    OSSUtils.upload(fileVo.getFileName(), fileVo.getFilePath());
////                } else {
//                    if (StringUtils.isEmpty(pdfUrl)) {
//                        pdfUrl = info.get("pdfUrl").toString();
//                        if (!FileUtils.getFileExt(pdfUrl).equals("pdf") && info.containsKey("backupUrl")) {
//                            if (StringUtils.isNotNull(info.get("backupUrl")) && StringUtils.isNotEmpty(info.get("backupUrl").toString())) {
//                                pdfUrl = info.get("backupUrl").toString();
//                            }
//                        }
//                    }
//                    fileVo = FileUtils.download(pdfUrl, fileConfig.getBasePath(), fileConfig.getBucketName());
//                    OSSUtils.upload(fileVo.getFileName(), fileVo.getFilePath());
//                    List<FileVo> fileVos = FileUtils.pdfToImage(new File(fileVo.getFilePath()), fileConfig.getBasePath(), fileConfig.getBucketName());
//                    for (FileVo f : fileVos) {
//                        OSSUtils.upload(f.getFileName(), f.getFilePath());
//                    }
//                    fileVo.setChildren(fileVos);
////                }
///**            TickData tickData = ocrUtils.checkReport(fileVo, false);
// //            fileVo.setTexts(tickData.getTexts());
// //            fileVo.setBoxes(tickData.getTexts());**/
//
//            } catch (Exception e) {
////                fileVo = new FileVo() {{
////                    setApiUrl(info.get("imageUrl").toString());
////                }};
//                log.error("{} 图片下载出错：", info, e);
//            }
//            info.put("fileVo", fileVo);
//            invoiceList.add(info);

        }

        return new TaskResult() {{
            setStatus(1);
            setOriginalData(new HashMap<String, Object>() {{
                put(invoiceFieldKey, invoiceList);
            }});
            setData(new HashMap<String, Object>() {{
                put(invoiceFieldKey, commonHandler.handleData(new HandlerData() {{
                    setTickCate(invoiceFieldKey);
                    setTicketType(task.getTicketType());
                    setOriginalData(invoiceList);
                }}).getResult());
            }});
        }};
    }
}
